package com.ag.hmk.action;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;

import com.ag.bean.TbUri;
import com.ag.common.AnnexPath;
import com.ag.common.BaseAction;
import com.ag.lzm.dao.TbUriDAO;
import com.opensymphony.xwork2.ActionContext;
/**
 * 
 * @author hmk
 * 文件上传处理
 */
public class UploadFileAction extends BaseAction implements SessionAware {

	private static final long serialVersionUID = 16777777L;
	
	@Resource(name = "LZMTbUriDAO")
	private TbUriDAO uriDAO;
	
	private List<File> Filedata;
	private List<String> FiledataFileName;
	private List<String> FiledataContentType;
	
	private int saveType;

	private Map<String, Object> session;

	/*
	 * 文件上传
	 */
	public String doUpload() {
		ActionContext ac = ActionContext.getContext();
		HttpServletRequest request = ServletActionContext.getRequest();
		ServletContext sc = (ServletContext) ac
				.get(ServletActionContext.SERVLET_CONTEXT);
		String savePath = sc.getRealPath("/");
		savePath = savePath + "hmkuploads/";
		File f1 = new File(savePath);
		if (!f1.exists()) {
			f1.mkdirs();
		}
		String fileUL ="";  //F:MYTEST/";
		try {
			switch (saveType) {
			case 0: //查询法规保存路径
					TbUri uri0= uriDAO.findById(AnnexPath.LAW_ANNEX_PATH);
					fileUL = uri0.getUPath();
					System.out.println("fileul===:"+fileUL);
				break;
			case 1: //查询案例保存路径
					TbUri uri1= uriDAO.findById(AnnexPath.CASE_ANNEX_PATH);
					fileUL = uri1.getUPath();
					System.out.println("fileul===:"+fileUL);
				break;
			case 2: //查询专家头像路径
					TbUri uri2= uriDAO.findById(AnnexPath.EXPERT_IMG_ANNEX_PATH);
					fileUL = uri2.getUPath();
					System.out.println("fileul===:"+fileUL);
				break;
			default:
				fileUL=savePath;
				break;
			}
		} catch (Exception e) {
			e.printStackTrace();
			fileUL=savePath;
		}
		
		File f2 = new File(fileUL);
		if (!f2.exists()) {
			f2.mkdirs();
		}
		
		int size = Filedata.size();
		if (size == 0)
			return ERROR;
		String extName = null;
		String name = null;
		for (int i = 0; i < size; i++) {
			extName = FiledataFileName.get(i).substring(
					FiledataFileName.get(i).lastIndexOf("."));
			name = UUID.randomUUID().toString();
			File file = new File(fileUL + name + extName);
			try {
				FileUtils.copyFile(Filedata.get(i), file);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		HttpServletResponse response = ServletActionContext.getResponse();
		try {
			response.getWriter().print(name + extName);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	public List<File> getFiledata() {
		return Filedata;
	}

	public void setFiledata(List<File> filedata) {
		Filedata = filedata;
	}

	public List<String> getFiledataFileName() {
		return FiledataFileName;
	}

	public void setFiledataFileName(List<String> filedataFileName) {
		FiledataFileName = filedataFileName;
	}

	public List getFiledataContentType() {
		return FiledataContentType;
	}

	public void setFiledataContentType(List<String> filedataContentType) {
		FiledataContentType = filedataContentType;
	}


	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	public String getAllFile() {
		return null;
	}

	

	public void setSaveType(int saveType) {
		this.saveType = saveType;
	}

}
